Skip to content

ENH: Arithmetic with Timestamp-based intervals #36001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from

Conversation

souris-dev
Copy link
Contributor

@souris-dev souris-dev changed the title ENH: Arithmetic with Timestamp-based intervals (pandas-dev#35908) ENH: Arithmetic with Timestamp-based intervals Aug 31, 2020
@souris-dev souris-dev marked this pull request as draft August 31, 2020 04:18
interval - pd.Timestamp("1900-01-01")

This is valid for addition using `+`, and also when the ends are :class:`Timedelta` s.
Intervals having ends as Timestamps can also get "added to" or get "subtracted from",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this? Timestamps can't be added/subtracted even if they're not the bounds of an interval

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Timestamps can be subtracted but not added (addition makes no sense). Subtracting Timestamps give Timedeltas.

Copy link
Member

@arw2019 arw2019 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @souris-dev for the PR!

Looks good, a few comments in the code

Re: tests generally I think you want to use pytest.mark.parametrize to make the tests easier to read

Re: docs you could add an example for the pandas.Interval API reference

Also, how does this work with Timestamps with different timezone info? Probably no need to mention in the docs but could add tests

@@ -184,6 +184,184 @@ def test_math_sub(self, closed):
with pytest.raises(TypeError, match=msg):
interval - "foo"

def test_math_sub_interval_timestamp_timestamp(self, closed):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd name this test_subtraction_interval_mixed_timestamp_timedelta

result -= Timedelta("3 days 01:00:00")
assert result == expected

def test_math_add_interval_timestamp_timestamp(self, closed):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_interval_addition_timestamp
(left and right bounds have to be the same type)

with pytest.raises(TypeError, match=msg):
interval += Timestamp("2002-01-08")

def test_math_sub_interval_timedelta_timestamp(self, closed):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_math_interval_mixed_timedelta_timestamp

)
assert result == expected

def test_math_add_interval_timestamp_timedelta(self, closed):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_addition_interval_mixed_timestamp_timedelta

result += Timedelta("1 days 00:00:00")
assert result == expected

def test_math_add_interval_timedelta_timedelta(self, closed):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_addition_interval_timedelta

)
assert result == expected

def test_math_sub_interval_timestamp_timedelta(self, closed):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_subtraction_interval_mixed_timestamp_timedelta

@souris-dev
Copy link
Contributor Author

souris-dev commented Aug 31, 2020

@arw2019 - Thanks for the review!
I'll try incorporating these.
Also, the point you've raised about different timezones is interesting, I'll have a look at that.

@jreback jreback added Interval Interval data type Numeric Operations Arithmetic, Comparison, and Logical operations Datetime Datetime data dtype labels Sep 5, 2020
Arithmetic with Timestamp and Timedelta-based Intervals
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Arithmetic can now be performed on :class:`Interval` s having their left and right
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason for all the spaces between backtick and "s"?

@@ -413,6 +415,8 @@ cdef class Interval(IntervalMixin):
isinstance(y, numbers.Number)
or PyDelta_Check(y)
or is_timedelta64_object(y)
or isinstance(y, _Timestamp)
or isinstance(y, _Timedelta)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the PyDelta_Check call makes the _Timedelta check extraneous. The remaining isinstance checks can be combined.

@@ -395,6 +395,8 @@ cdef class Interval(IntervalMixin):
isinstance(y, numbers.Number)
or PyDelta_Check(y)
or is_timedelta64_object(y)
or isinstance(y, _Timestamp)
or isinstance(y, _Timedelta)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is covered by L397

if the ends were numeric (:issue:`35908`).

Arithmetic can be performed by directly using arithmetic operators (`-` or `+`),
so something like this will work:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add something like this as an example in Interval

@github-actions
Copy link
Contributor

This pull request is stale because it has been open for thirty days with no activity. Please update or respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Oct 14, 2020
Copy link
Member

@arw2019 arw2019 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@souris-dev is this active?

@jreback
Copy link
Contributor

jreback commented Feb 11, 2021

closing as stale. if you want to continue, pls ping and can re-open.

@jreback jreback closed this Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Interval Interval data type Numeric Operations Arithmetic, Comparison, and Logical operations Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: Arithmetic with Timestamp-based intervals
4 participants